<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        $.ajax = function (opt) {
            let opts = {
                url: opt.url,
                success: opt.success || null,
                err: opt.err || function () { },
                type: opt.type || "get",
                async: opt.async || true,
                data: opt.data || null
            };
            let xhr = new XMLHttpRequest();//创建Xmlhttp请求实例
            xhr.open("GET", "./data/ajax.txt", true);//
            console.log("xhr", xhr);//http状态码
            xhr.onreadystatechange = function () {
                console.log("xhr.readyState", xhr.readyState);
                //        0 请求未初始化
                //  1 服务器建立连接
                //  2 请求已经接受
                //  3 请求处理中
                //  4 请求已完成, 响应已就绪
                if (xhr.readyState === 4) {
                    console.log("xhr.status", xhr);//http状态码
                    console.log("xhr.status", xhr.status)

                    switch (xhr.status) {
                        case 200: {//正常接受到服务器数据
                            document.getElementById("text").innerHTML = xhr.responseText;
                            opts.success && opts.success();
                            break
                        }
                        case 300: {//正常接受到服务器数据
                            //                    xhr.responseText;
                            break
                        }
                        case 400: {
                            break
                        }
                        case 500: {
                            break
                        }
                    }
                }
            };
            xhr.send(opts.data)
        }
    </script>
</body>

</html>